/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function(){
	
	jQuery("#list2").jqGrid({
	   	url:'/admin/users/listusers',
		datatype: "json",
	   	colNames:['User Id','User Name', 'Role', 'DateCreated','Is Active'],
	   	colModel:[
	   		{name:'id',index:'id', width:55,align:"center" ,formatter: 'integer'},
	   		{name:'username',index:'username', width:120,align:"center"},
	   		{name:'role',index:'role', width:100,align:"center",stype:'select', editoptions:{value:":All;parent:Parent;administrator:Administrator"}},
	   		{name:'date_created',index:'date_created', width:100, align:"center" , formatter:'date' , formatoptions:{srcformat:"Y-m-d",newformat:"d-M-Y"},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd'});} } },
	   		{name:'is_active',index:'is_active', width:120, align:"center" , formatter:'checkbox',stype:'select', editoptions:{value:":All;0:0;1:1"}}	
	   	],
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	pager: '#pager2',
	   	sortname: 'id',
	    viewrecords: true,
	    sortorder: "desc",
	    caption:"Users List",
	    multiselect: true,
	    autowidth:true,
	    gridview:true,
	    height:"auto",
	    rownumbers: true
	});
	jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});
	jQuery("#list2").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : true});
	jQuery("#list2").jqGrid('navButtonAdd','#pager2',{caption:"Add", onClickButton:function(){
		
		$("#add-users-data").show();
	} });
/*
 * class List 
 */
	
		jQuery("#class_list").jqGrid({
		   	url:'/admin/classes/classlist',
			datatype: "json",
		   	colNames:['Class Id','Class Title', 'Created On', 'Last Updated On','Is Active'],
		   	colModel:[
		   		{name:'id',index:'id', width:55,align:"center" ,formatter: 'integer'},
		   		{name:'class_name',index:'class_name', width:120,align:"center"},
		   		{name:'created',index:'created', width:100,align:"center",formatter:'date' , formatoptions:{srcformat:"Y-m-d",newformat:"d-M-Y"},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd'});} } },
		   		{name:'updated',index:'updated', width:100, align:"center" , formatter:'date' , formatoptions:{srcformat:"Y-m-d",newformat:"d-M-Y"},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd'});} } },
		   		{name:'is_active',index:'is_active', width:120, align:"center" , formatter:'checkbox',stype:'select', editoptions:{value:":All;0:0;1:1"}}	
		   	],
		   	rowNum:10,
		   	rowList:[10,20,30],
		   	pager: '#class_list_pager',
		   	sortname: 'id',
		    viewrecords: true,
		    sortorder: "asc",
		    caption:"Class List",
		    multiselect: true,
		    autowidth:true,
		    gridview:true,
		    height:"auto",
		    rownumbers: true
		});
		jQuery("#class_list").jqGrid('navGrid','#class_list_pager',{edit:false,add:false,del:false});
		jQuery("#class_list").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : true});

/*
 * Academic Details
 *
 */

jQuery("#academic_list").jqGrid({
		   	url:'/admin/academic/academiclist',
			datatype: "json",
		   	colNames:['Id','Start Date', 'End Date', 'Created on'],
		   	colModel:[
		   		{name:'id',index:'id', width:55,align:"center" ,formatter: 'integer'},
		   		{name:'start_date',index:'start_date', width:100,align:"center",formatter:'date' , formatoptions:{srcformat:"Y-m-d",newformat:"d-M-Y"},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd'});} } },
		   		{name:'end_date',index:'end_date', width:100,align:"center",formatter:'date' , formatoptions:{srcformat:"Y-m-d",newformat:"d-M-Y"},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd'});} } },
		   		{name:'created',index:'created', width:100, align:"center" , formatter:'date' , formatoptions:{srcformat:"Y-m-d",newformat:"d-M-Y"},searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd'});} } }
		   	],
		   	rowNum:10,
		   	rowList:[10,20,30],
		   	pager: '#academic_list_pager',
		   	sortname: 'id',
		    viewrecords: true,
		    sortorder: "asc",
		    caption:"Academic List",
		    multiselect: true,
		    autowidth:true,
		    gridview:true,
		    height:"auto",
		    rownumbers: true
		});
		jQuery("#academic_list").jqGrid('navGrid','#academic_list_pager',{edit:false,add:false,del:false});
		jQuery("#academic_list").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : true});

	/*
	
	$("#flex1").flexigrid
	(
	{
	url: '/admin/users/listusers',
	dataType: 'json',
	colModel : [
		{display: 'Delete', name : 'delete', width : 40, sortable : true, align: 'center'},
		{display: 'First Name', name : 'firstname', width : 180, sortable : true, align: 'left'},
		{display: 'Last Name', name : 'lastname', width : 120, sortable : true, align: 'left'},
		{display: 'Username', name : 'username', width : 120, sortable : true, align: 'left'},
		{display: 'Is Active', name : 'isactive', width : 120, sortable : true, align: 'left'},
		{display: 'Date Created', name : 'datecreated', width : 120, sortable : true, align: 'left'},

		],
	buttons : [
		{name: 'Add', bclass: 'add', onpress : adduser},
		{name: 'Delete', bclass: 'delete', onpress : test},
		{separator: true}
		],
	searchitems : [
		{display: 'firstname', name : 'firstname',isdefault: true},
		{display: 'username', name : 'username', isdefault: false}
		],
	sortname: "iso",
	sortorder: "asc",
	usepager: true,
	title: 'Users List',
	useRp: true,
	rp: 15,
	showTableToggleBtn: true,
	width: '100%',
	height: 200
	}
	);
	*/
	function adduser()
	{
		$('#add-users-data').show();
	}
	function test(com,grid)
	{
		if (com=='Delete')
			{
				confirm('Delete ' + $('.trSelected',grid).length + ' items?')
			}
		else if (com=='Add')
			{
				alert('Add New Item');
			}			
	}
   
    var options = {
        // target:        '#output2',   // target element(s) to be updated with server response
        beforeSubmit:  addUsersShowRequest,  // pre-submit callback
        success:       addUsersShowResponse,  // post-submit callback

        // other available options:
        //url:       url         // override for form's 'action' attribute
        //type:      type        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type)
        //clearForm: true        // clear all form fields after successful submit
        resetForm: true        // reset the form after successful submit

    // $.ajax options can be used here too, for example:
    //timeout:   3000
    };

    $('#addusersform').submit(function() {
        // inside event callbacks 'this' is the DOM element so we first
        // wrap it in a jQuery object and then invoke ajaxSubmit
        $(this).ajaxSubmit(options);

        // !!! Important !!!
        // always return false to prevent standard browser submit and page navigation
        return false;
    });

   //admission form validation

   $("#admissionsform").validate({
		rules: {
			p_firstname: {
				required: true
			},
			p_mobileno: {
				required: true,
				minlength: 10
			},
			s_firstname: {
				required: true,
				minlength: 5
			},
			s_admission_no: {
				required: true,
			},
			s_class: {
				required: true
			},
			s_rollno: {
				required: true,
				minlength: 2
			},
		messages: {
			p_firstname:{
				required: "Please enter parents first name"
			},
			p_mobileno: {
				required: "Please enter a valid Mobile No",
				minlength: "Your username must consist of at least 10 characters"
			},
			s_firstname: {
				required: "Please provide students first name",
				minlength: "Your password must be at least 5 characters long"
			},
			s_admission_no: {
				required: "Please provide a admission no",
			},
			s_class: "Please select a class",
			s_rollno: "Please enter student roll no"
		}
		}


    });

    //validation for classes form
    $('#classesform').validate({
		rules: {
			p_classname: {
				required: true
			},
		messages: {
			p_classname:{
				required: "Please enter parents first name"
			}
                    }
		}
    });
var options_class = {
        // target:        '#output2',   // target element(s) to be updated with server response
        beforeSubmit:  addClassShowRequest,  // pre-submit callback
        success:       addClassShowResponse,  // post-submit callback

        // other available options:
        //url:       url         // override for form's 'action' attribute
        //type:      type        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type)
        clearForm: true        // clear all form fields after successful submit
        //resetForm: true        // reset the form after successful submit

    // $.ajax options can be used here too, for example:
    //timeout:   3000
    };
    $('#subjectform').ajaxForm(options_class);
    
    
    
    
    //validation for subject form
    $('#subjectform').validate({
		rules: {
			p_subjectname: {
				required: true
			},
		messages: {
			p_subjectname:{
				required: "Please enter parents first name"
			}
                    }
		}
    });
var options_class = {
        // target:        '#output2',   // target element(s) to be updated with server response
        beforeSubmit:  addSubjectShowRequest,  // pre-submit callback
        success:       addSubjectShowResponse,  // post-submit callback

        // other available options:
        //url:       url         // override for form's 'action' attribute
        //type:      type        // 'get' or 'post', override for form's 'method' attribute
        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type)
        clearForm: true        // clear all form fields after successful submit
        //resetForm: true        // reset the form after successful submit

    // $.ajax options can be used here too, for example:
    //timeout:   3000
    };
    $('#subjectform').ajaxForm(options_class);
    
    
    
});


//class
function addSubjectShowResponse(responseText, statusText, xhr, $form)  {
    // for normal html responses, the first argument to the success callback
    // is the XMLHttpRequest object's responseText property

    // if the ajaxSubmit method was passed an Options Object with the dataType
    // property set to 'xml' then the first argument to the success callback
    // is the XMLHttpRequest object's responseXML property

    // if the ajaxSubmit method was passed an Options Object with the dataType
    // property set to 'json' then the first argument to the success callback
    // is the json data object returned by the server
    if(responseText)
    {
        if(responseText.login == true)
        {
            if(responseText.result == 'success')
            {
            	// $("#class_list").trigger("reloadGrid"); 
            }
        }

    }
}

function addSubjectShowRequest(formData, jqForm, options) {

    if($('#subjectform').valid())
    {
        return true;
    }
    return false;
    // formData is an array; here we use $.param to convert it to a string to display it
    // but the form plugin does this for you automatically when it submits the data
    var queryString = $.param(formData);

    // jqForm is a jQuery object encapsulating the form element.  To access the
    // DOM element for the form do this:
    // var formElement = jqForm[0];

    alert('About to submit: \n\n' + queryString);

    // here we could return false to prevent the form from being submitted;
    // returning anything other than false will allow the form submit to continue
    return true;
}


// class
function addClassShowResponse(responseText, statusText, xhr, $form)  {
    // for normal html responses, the first argument to the success callback
    // is the XMLHttpRequest object's responseText property

    // if the ajaxSubmit method was passed an Options Object with the dataType
    // property set to 'xml' then the first argument to the success callback
    // is the XMLHttpRequest object's responseXML property

    // if the ajaxSubmit method was passed an Options Object with the dataType
    // property set to 'json' then the first argument to the success callback
    // is the json data object returned by the server
    if(responseText)
    {
        if(responseText.login == true)
        {
            if(responseText.result == 'success')
            {
            	 $("#class_list").trigger("reloadGrid"); 
            }
        }

    }
}

function addClassShowRequest(formData, jqForm, options) {

    if($('#classesform').valid())
    {
        return true;
    }
    return false;
    // formData is an array; here we use $.param to convert it to a string to display it
    // but the form plugin does this for you automatically when it submits the data
    var queryString = $.param(formData);

    // jqForm is a jQuery object encapsulating the form element.  To access the
    // DOM element for the form do this:
    // var formElement = jqForm[0];

    alert('About to submit: \n\n' + queryString);

    // here we could return false to prevent the form from being submitted;
    // returning anything other than false will allow the form submit to continue
    return true;
}
function addUsersShowRequest(formData, jqForm, options) {

    if($('#addusersform').valid())
    {
        return true;
    }
    return false;
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}

function addUsersShowResponse(responseText, statusText, xhr, $form)  {
    // for normal html responses, the first argument to the success callback
    // is the XMLHttpRequest object's responseText property

    // if the ajaxSubmit method was passed an Options Object with the dataType
    // property set to 'xml' then the first argument to the success callback
    // is the XMLHttpRequest object's responseXML property

    // if the ajaxSubmit method was passed an Options Object with the dataType
    // property set to 'json' then the first argument to the success callback
    // is the json data object returned by the server
    if(responseText)
    {
        if(responseText.login == 'true')
        {
            if(responseText.result == 'success')
            {

        }
        }

    }
} 